const { Router } = require('express');
let expess = require('express');
let router = expess.Router()
let ejs = require("ejs")

let mgdb = require('../../utils/mgdb')

// router.set('view engine', 'html');

//所有列表接口
router.get('/:listname',(req,res,next)=>{
  console.log("123")

  // console.log('news/xx',req.query._page,req.query._limit,req.body.q,req.headers._sort);

  // console.log("5")
  let collectionName = req.params.listname;
  let {_page,_limit,_sort,q} = req.query;

  mgdb.findList({collectionName,_page,_sort,_limit,q}).then(
    result => {
      // router.get()
      console.log("----------")
      // console.log(result)
      console.log(result)
      let data = result.data
      let dataa = data
      // router.get()
      res.send(result)



      // ejs.renderFile("./views/shop-grid.ejs",{data},(err,data)=>{
      //   if(err){ 
      //     console.log("渲染失败")
      //     res.send(err)
      //   }else{
      //     console.log("渲染成功")
      //     // console.log(data)
      //     // res.send(data)
      //     res.send(dataa)
      //   }
      // })





      // res.send(result)

    }
  ).catch(
    err => res.send(err) 
  )

})

//所有详情结构
router.get('/:listname/:_id',(req,res,next)=>{
  // console.log('news/xx/id',req.query._page,req.query._limit,req.body.q,req.headers._sort)
  let collectionName = req.params.listname;
  let _id = req.params._id;

  mgdb.findDetail({collectionName,_id}).then(
    result => res.send(result)
  ).catch(
    err => res.send(err) 
  )
})


module.exports = router;